package cn.fastoo.web.backstage.web.controller.order;

import cn.fastoo.common.dao.order.OrderBaseDao;
import cn.fastoo.common.entity.order.OrderBase;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.web.backstage.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by zhengzheng on 2018/6/20.
 */
@Controller
@RequestMapping("/order")
public class OrderBaseController {

    @Autowired
    private OrderBaseDao dao;

    @RequestMapping
    public String index() {
        return "/order/index";
    }

    @RequestMapping("订单详情")
    public String detail() {
        return "/order/订单详情";
    }

    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute OrderBase newData, QuerySearch querySearch, String op) {
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                newData.setCreatedTime(new Date());
                newData.setUpdateTime(new Date());
                newData.setCreatedMan(ShiroUtils.getName());
                newData.setUpdateMan(ShiroUtils.getName());
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                OrderBase oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    oldData.setMemberId(newData.getMemberId());
                    oldData.setMemberGuardianId(newData.getMemberGuardianId());
                    oldData.setStatus(newData.getStatus());
                    oldData.setUpdateMan(ShiroUtils.getName());
                    oldData.setUpdateTime(new Date());
                    dao.update(oldData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.findPage(querySearch.getSearch(),ShiroUtils.getUserId()),querySearch);
    }


    @RequestMapping("info")
    @ResponseBody
    public String info(@RequestParam Long id){
        return JsonResult.success(dao.findById(id));
    }
}